# -*- coding: utf-8 -*-
##############################################################################
#    
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
#
##############################################################################
from openerp.osv import fields,osv,expression
from openerp.tools.translate import _
from openerp.addons.dmp_mrp_produce.mrp_produce import mrp_production as mrp_production_super
import openerp.addons.decimal_precision as dp
from datetime import datetime
from openerp.tools.float_utils import float_compare
from openerp import netsvc

#工单质量问题
class mrp_production(osv.osv):
    _inherit = 'mrp.production'
    
    def _mrp_equips(self, cr, uid, ids, field_names, args, context=None):
        res = {} 
        for id in ids:
            res[id] = []
        for order in self.browse(cr, uid, ids, context=context):
            if not order.product_id.mrp_equip_ids:
                continue
            res[id] = [equip.id for equip in order.product_id.mrp_equip_ids]
        return res
    
    _columns = {
        'mrp_equip_ids': fields.function(_mrp_equips, type='one2many', relation='product.product', string='相关设备工装'),
        #增加equip
        'state': fields.selection(
            [('draft', 'New'), ('cancel', 'Cancelled'), ('cancel_except', '异常取消'),('picking_except', 'Picking Exception'), 
                ('approve', 'Approving'), 
                ('equip', '工装检查'),
                ('confirmed', 'Awaiting Raw Materials'),
                ('ready', 'Ready to Produce'), ('in_production', 'Production Started'), ('pause','暂停'), ('done', 'Done'), ('done_except', '异常完成')],                                  
            string='Status', readonly=True,
            track_visibility='onchange',
            help="When the production order is created the status is set to 'Draft'.\n\
                If the order is confirmed the status is set to 'Waiting Goods'.\n\
                If any exceptions are there, the status is set to 'Picking Exception'.\n\
                If the stock is available then the status is set to 'Ready to Produce'.\n\
                When the production gets started then the status is set to 'In Production'.\n\
                When the production is over, the status is set to 'Done'."),        
        }
        
    #工装审批
    def action_equip(self, cr, uid, ids, context=None):
        self.write(cr, uid, ids, {'state':'equip'})
        return True